<?php
apf_require_controller('Oa_Attendance_Abstract');
apf_require_class('Bll_Attendance_AccessDayRecord');
class Oa_Attendance_UnauditController extends Oa_Attendance_AbstractController {

    const PAGEROW = 10;
    private $bll;
    private $user_info;

    public function handle_request() {
        $user_info = $this->get_user_info();
        $user_id = $user_info['user_id'];
        $this->get_bll();

        $params = $this->request->get_parameters();
        $where = $params['keywords'];
        $start_time = $params['start_time'];
        $end_time = $params['end_time'];
        $page_num = $params['p'];

        if($page_num <= 0){$page_num = 1;}
        $offset = ($page_num-1)*self::PAGEROW;
        apf_require_class('Bll_Attendance_Audit');
        $bll = new Bll_Attendance_Audit();
        $data = $bll->get_audit_list($user_id,self::PAGEROW,$offset,$where,$start_time,$end_time);

        if(!empty($data['rows'])){
            foreach($data['rows'] as $k=>$val){
                if($val['type']=='缺勤'){
                   $data['rows'][$k]['leave_time'] = $this->_get_late_info( $val['user_id'], substr($val['start_time'],0,10));
                   if(empty($data['rows'][$k]['leave_time'])){
                       $data['rows'][$k]['leave_time'] = 0;
                   }
                }else{
                   $data['rows'][$k]['leave_time'] =abs($val['leave_time']);
                }
                $data['rows'][$k]['apply_date_str'] = date('Y-m-d H:i',$val['apply_time_ext']);
                $tmp_day = substr($val['start_time'], 0,10);
                $data['rows'][$k]['access_date'] = $tmp_day;
                $data['rows'][$k]['day_record'] = $this->get_late_info($val['user_id'],$tmp_day);
            }
        }

        $rows = $data['rows'];
        $this->request->set_attribute('rows',$rows);
        $total_num = $data['count'];//总行数

        $multipage = $this->build_multiPage_info($total_num,$page_num,$params);
        $this->request->set_attribute('multipage',$multipage);
        $this->request->set_attribute('params',$params);
        return 'Oa_Attendance_Unaudit';
    }
    //缺勤
    private function _get_late_info($uid,$day){
        $day = str_replace('-','',$day);
        $re_bll = new Bll_Attendance_AccessDayRecord();
        $data = $re_bll->get_user_day($uid,$day);
        if(!empty($data)){
            $data = $data[0];
            $data['status'] = $re_bll->check_day_work_status($day,$data['start_work'],$data['end_work']);
            $data['start_work']=substr($data['start_work'], 0,2).':'.substr($data['start_work'], 2,2);
            $data['end_work']=substr($data['end_work'], 0,2).':'.substr($data['end_work'], 2,2);

            if ($data['status'][0] == 3 && $data['status'][1] == 4) {
                //全天缺勤;
                $data['day_type'] = '全天';
                $data['day'] = 1;
            }elseif ($data['status'][0] == 3){
                //上午缺勤
                $data['day_type'] = '上午';
                $data['day'] = 0.5;
            }elseif ($data['status'][0] == 4){
                //下午缺勤
                $data['day_type'] = '下午';
                $data['day'] = 0.5;
            }

        }
        return $data['day'];
    }

    /**
     * 获取翻页
     * @param unknown_type $totalnum 总的行数
     * @param unknown_type $page_num 当前第几页
     * @param array $url_params
     * @param int $uid 用户ID
     */
    public function build_multiPage_info($totalnum,$page_num = 0,$url_params = array()) {
        apf_require_class('Tools_MultiPage');
        //翻页信息
        $url = "/unaudit/";
        $total_rows = $totalnum;
        if($page_num <= 0){$page_num = 1;}
        $url_params['p'] = $page_num;

        $multipage = Tools_MultiPage::multiPage($url,$url_params, '', $total_rows, self::PAGEROW, 10);
        return $multipage;
    }

    private function get_late_info($uid,$day){
        $day = str_replace('-','',$day);
        $re_bll = new Bll_Attendance_AccessDayRecord();
        $data = $re_bll->get_user_day($uid,$day);
        if(!empty($data)){
            $data = $data[0];
            $data['status'] = $re_bll->check_day_work_status($day,$data['start_work'],$data['end_work']);
            $data['start_work']=substr($data['start_work'], 0,2).':'.substr($data['start_work'], 2,2);
            $data['end_work']=substr($data['end_work'], 0,2).':'.substr($data['end_work'], 2,2);
        }
        return $data;
    }

    private function get_user_info(){
        $this->session_act();
        $this->user_info = $_SESSION;
        return $this->user_info;
    }
    private function get_bll(){
        apf_require_class('Bll_Attendance_Unaudit');
        $bll = new Bll_Attendance_Unaudit();
        $bll->enable_cache = false;
        $this->bll = $bll;
    }

}